package cn.hd.controller;

import cn.hd.entity.Fileupload;
import cn.hd.entity.License;
import cn.hd.entity.Result;
import cn.hd.service.LicenseService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@CrossOrigin
public class LicenseController {
    @Autowired
    private LicenseService licenseService;

    //一对多，分页查询
    @RequestMapping("/selectAllFenYeLicense")
    public Object selectAllFenYeLicense(int pageNum,int pageSize){
        PageInfo<License> page = licenseService.selectAllFenYeLicense(pageNum, pageSize);
        if(page.getList().size()!=0){
            return new Result("200","查询成功！",page);
        }else {
            return new Result("400","查询失败！",null);
        }
    }

    //把每个认证类型对应的资质文件添加到中间表中
    @RequestMapping("/addFileuploadLicense")
    public Result addFileuploadLicense(long licenseId, long fileuploadId){
        int i = licenseService.addFileuploadLicense(licenseId, fileuploadId);
        if(i!=0){
            return new Result("200","添加成功！",null);
        }else{
            return new Result("400","添加失败！",null);
        }
    }

    //根据编号查询认证类型及对应的资质文件
    @RequestMapping("/selectLicenseById")
    public Object selectLicenseById(long licenseId){
        Fileupload fileupload = licenseService.selectLicenseById(licenseId);
        if(fileupload!=null){
            return new Result("200","查询成功！",fileupload);
        }else {
            return new Result("400","查询失败！",null);
        }
    }

    //根据认证类型编号删除中间表中对应的资质文件
    @RequestMapping("/deleteFileuploadLicense")
    public Result deleteFileuploadLicense(long licenseId){
        int i = licenseService.deleteFileuploadLicense(licenseId);
        if(i!=0){
            return new Result("200","删除成功！",null);
        }else{
            return new Result("400","删除失败！",null);
        }
    }

    //根据编号查询认证类型对象
    @RequestMapping("selectLicenseByLicenseId")
    public Object selectLicenseByLicenseId(long license_id){
        License license = licenseService.selectLicenseByLicenseId(license_id);
        if(license==null){
            return new Result("400","查询失败",null);
        }else{
            return new Result("200","查询成功",license);
        }
    }
}
